﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using OneSoft.EntityCore;
using OneSoft.DataAccess;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;

namespace BySoft.Server.DataAccess
{
    public partial class SysFormauditDA
    {
        private static string GetHeadSQL()
        {
            return @"SELECT A.*,b.ModuleName,c.ParameterName AS IsCheckName 
FROM SYS_FormAudit A 
LEFT JOIN SYS_Module b ON b.ModuleID=a.ModuleID
LEFT JOIN SYS_Parameter c ON c.ParameterTypeID='YesNo' AND c.ParameterCode=a.IsCheck
WHERE 1=1";
        }

		private static string GetBodySQL()
        {
            return @"SELECT a.*,b.UserName AS CheckUserName 
FROM SYS_FormAuditDetail a
LEFT JOIN SYS_User b ON b.UserID=a.CheckUser
WHERE 1=1
";
        }

        public static DataTable SelectList(string strModuleID,string strIsCheck,
                                                      int pageIndex,
                                                      int pageSize,
                                                      out int count)
        {
            try
            {
                #region SQL Server
                string strcmd = GetHeadSQL();

                DataAccessParameterCollection dpc = new DataAccessParameterCollection();

                if (!string.IsNullOrEmpty(strModuleID))
{
    strcmd += " and b.ModuleName LIKE @ModuleName";
    dpc.AddWithValue("@ModuleName", "%" + strModuleID + "%");
}

if (!string.IsNullOrEmpty(strIsCheck))
{
    strcmd += " and A.IsCheck = @IsCheck";
    dpc.AddWithValue("@IsCheck", strIsCheck);
}



                strcmd += " ORDER BY A.ModifyDate DESC";

                return DataAccessCommon.ExecuteDataTablePaging(strcmd, pageIndex, pageSize, dpc, out count);
                #endregion
            }
            catch
            {
                throw;
            }
        }

        public static DataTable SelectHead(string strCode)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    #region SQL Server
                    DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                    string strcmd = GetHeadSQL();
                    if (!string.IsNullOrEmpty(strCode))
                    {
                        strcmd += " AND A.FormAuditID=@FormAuditID";
                        dpc.AddWithValue("@FormAuditID", strCode);
                    }
                    return broker.FillSQLDataSet(strcmd, dpc).Tables[0];
                    #endregion
                }
                catch
                {
                    throw;
                }
            }
        }

		public static DataTable SelectBody(string strCode)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    return SelectBody(strCode,broker);
                }
                catch
                {
                    throw;
                }
            }
        }

        public static DataTable SelectBody(string strCode, DataAccessBroker broker)
        {
             DataAccessParameterCollection dpc = new DataAccessParameterCollection();
             string strcmd = GetBodySQL() + " AND A.FormAuditID=@FormAuditID";
             dpc.AddWithValue("@FormAuditID", strCode);
             return broker.FillSQLDataSet(strcmd, dpc).Tables[0];
        }

        public static DataTable CheckUsed(string strCode)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    string strcmd = "select '' as tablename";
                    //                    string strcmd = @"SELECT t.tablename
                    //                                    FROM 
                    //                                    (
                    //	                                     SELECT COUNT(A.CustomerInfoNO) AS NUM,
                    //                                        '[用户表'+A.CustomerInfoNO AS tablename 
                    //FROM MD_CustomerInfo A WHERE A.SaleAreaId=@strCode 
                    //                                        group by A.CustomerInfoNO                  
                    //                                    ) t
                    //                                    WHERE t.NUM>0";
                    DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                    dpc.AddWithValue("@strCode", strCode);

                    return broker.FillSQLDataSet(strcmd, dpc).Tables[0];

                }
                catch
                {
                    throw;
                }
            }
        }
    }
}

